from django.urls import path
from . import views
from django.urls import reverse_lazy
from django.contrib.auth.views import (
    PasswordResetView, PasswordResetDoneView,
    PasswordResetConfirmView, PasswordResetCompleteView,
    PasswordChangeView, PasswordChangeDoneView
)
from django.views.generic.edit import UpdateView, DeleteView
from .models import Book

urlpatterns = [
    path('', views.index, name='index'),
    path('book/<int:pk>/', views.BookDetailView.as_view(), name='book-detail'),
    path('books/', views.BookListView.as_view(), name='books'),
    path('authors/', views.AuthorListView.as_view(), name='authors'),
    path('author/<int:pk>/', views.AuthorDetailView.as_view(), name='author-detail'),
    path('login/', views.UserLoginView.as_view(), name='user-login'),
    path('logout/', views.UserLogoutView.as_view(), name='user-logout'),
    path('register/', views.UserRegisterView.as_view(), name='user-register'),
    path('profile/', views.user_profile, name='user-profile'),
    path('staff-only/', views.StaffOnlyView.as_view(), name='staff-only'),
    path('password-reset/', PasswordResetView.as_view(
        template_name='catalog/password_reset.html',
        success_url='{% url "password_reset_done" %}',
        email_template_name='catalog/password_reset_email.html'
    ), name='password_reset'),
    path('password-reset/done/', PasswordResetDoneView.as_view(
        template_name='catalog/password_reset_done.html'
    ), name='password_reset_done'),
    path('password-reset/confirm/<uidb64>/<token>/', PasswordResetConfirmView.as_view(
        template_name='catalog/password_reset_confirm.html',
        success_url='{% url "password_reset_complete" %}'
    ), name='password_reset_confirm'),
    path('password-reset/complete/', PasswordResetCompleteView.as_view(
        template_name='catalog/password_reset_complete.html'
    ), name='password_reset_complete'),
    path('password-change/', PasswordChangeView.as_view(
        template_name='catalog/password_change.html',
        success_url=reverse_lazy('password_change_done')
    ), name='password_change'),
    path('password-change/done/', PasswordChangeDoneView.as_view(
        template_name='catalog/password_change_done.html'
    ), name='password_change_done'),
    path('my-borrowed/', views.LoanedBooksByUserListView.as_view(), name='my-borrowed'),
    path('author/create/', views.AuthorCreateView.as_view(), name='author-create'),
    path('author/<int:pk>/update/', views.AuthorUpdateView.as_view(), name='author-update'),
    path('author/<int:pk>/delete/', views.AuthorDeleteView.as_view(), name='author-delete'),
    path('book/<uuid:pk>/borrow/', views.BorrowBookView.as_view(), name='borrow-book'),
    path('book/<uuid:pk>/renew/', views.RenewBookView.as_view(), name='renew-book'),
    # 添加 book-update 和 book-delete 的 URL 模式
    path('book/<int:pk>/update/', UpdateView.as_view(
        model=Book,
        fields=['title', 'author', 'summary', 'isbn', 'genre'],
        template_name='catalog/book_form.html',
        success_url=reverse_lazy('books')
    ), name='book-update'),
    path('book/<int:pk>/delete/', DeleteView.as_view(
        model=Book,
        template_name='catalog/book_confirm_delete.html',
        success_url=reverse_lazy('books')
    ), name='book-delete'),
    path('book/<uuid:pk>/return/', views.ReturnBookView.as_view(), name='return-book'),
]
